class Emitter {
    constructor() {
        this.cache = {}
    }

    emit = (...args) => {
        let event = args[0]
        let params = [].slice.call(args, 1) // slice 确定起始位置
        this.cache[event].forEach(fn => fn(params))  // 不能用map map不会修改原数组，用的化要新赋值
    }

    on = (evnet, callback) => {
        this.cache[evnet].push(callback)
    }

    off = (event, callback) => {
        this.cache[event] = this.cache[event].filter(fn => {
            return fn !== callback  // 确定是非
        });
    }

    once = (event, callback) => {
        function wrapfn(...args) {  
            callback.call(this, args)
            this.off(evnet, wrapfn);  // 保证on/off function一致
        }
        this.on(event, wrapfn);  // 监听 
    }
}